class Solution {
public
:
    void flatten(TreeNode* root) 
{
    while
(root)
    {
       TreeNode* rem=root->right;
       if
(root->left)
    {
        TreeNode*p=root->left;
        while
(p->right)
        {
          p=p->right;
        }

         root->right=root->left;
         root->left=
nullptr
;
         p->right=rem;

    }

     root=root->right;


    }
 

    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
long long val=LLONG_MIN;
//int val=INT_MIN;
  
    bool _isValidBST(TreeNode* root) {
      if(root==nullptr)return true;
     if(! _isValidBST(root->left))
     return false;
   
     if(root->val<=val)return false;
     val=root->val;
      return _isValidBST(root->right);

       //return true;
      
    }

    bool isValidBST(TreeNode* root) {
       return  _isValidBST(root);
    }
};/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int>result;
    void _kthSmallest(TreeNode* root)
    {
      if(root==nullptr)
      {
         return ;
      }
       _kthSmallest(root->left);
       result.push_back(root->val);
       _kthSmallest(root->right);

    }





    int kthSmallest(TreeNode* root, int k) {
   
      _kthSmallest(root);
     
   


     return result[k-1];







    }
};